import CoinArea2D from "./obj/coin_area_obj";
import CoinRigidObj from "./obj/coin_rigid_obj";
import CoinStaticObj from "./obj/coin_static_obj";
import IconKObj from "./obj/iocn_k_obj";
import IconSprite from "./sprite/icon_spr";
import KnightSprite from "./sprite/knight_spr";
import MoveKnight from "./sprite/moveParent/move_knight";
import MovePlayer from "./sprite/moveParent/move_player";
import MoveKnightNode from "./sprite/move_node/move_knight_node";
import MovePlayerNode from "./sprite/move_node/move_player_node";
import { MoveKnightSignal } from "./sprite/move_signal/move_knight_signal";
import MovePlayerSignal from "./sprite/move_signal/move_player_signal";
import MoveSignal from "./sprite/move_signal/move_signal";
import PlayerSprite from "./sprite/player_spr";

// 游戏根节点(Game)
export default class Game extends godot.Node2D {
	
	// Declare member variables here. Examples:
	a = 2;
	b = "text";
	
	constructor() {
		super();
	}
	
	// Called when the node enters the scene tree for the first time.
	_ready() {
		console.log("ready----111启动完成");

		this.inputMapSet()

		
		// // Sprite节点
		// let spr = new godot.Sprite();
		// spr.texture = godot.load("res://icon.png") as godot.Texture;
		// spr.position = new godot.Vector2(200,200);

		// // 添加到根节点Game
		// this.add_child(spr);

		// this.moreSpr();
		// this.iconSpr()
		// this.testSpr()
		// this.knightSpr()
		// this.playerSpr()
		// this.inputMapInfo()

		// this.moveParent()
		// this.moveNode()
		// this.moveSignal()
		
		// this.initArea();
		// this.initKobj();
		// this.initStaticObj();
		this.initRigidObj()
	}
	
	// Called every frame. 'delta' is the elapsed time since the previous frame.
	_process(delta) {
		
	}
	
	_input(event: godot.InputEvent): void {
		// godot.print("Game Input",event);
	}

	moreSpr(){
		for (let index = 0; index < 5; index++) {
			let spr = new godot.Sprite();
			spr.texture = godot.load("res://icon.png") as godot.Texture;
			spr.position = new godot.Vector2(200+index*100,200);
			// 添加到根节点Game
			this.add_child(spr);
		}
	}

	iconSpr(){
		for (let index = 0; index < 5; index++) {
			let spr = new IconSprite(200,200+index*100)
			// 添加到根节点Game
			this.add_child(spr);
		}
	}

	testSpr(){
		let spr = new IconSprite(200,200)
			// 添加到根节点Game
			this.add_child(spr);
	}

	knightSpr(){
		let spr = new KnightSprite()
		this.add_child(spr);
	}

	playerSpr(){
		let spr = new PlayerSprite()
		this.add_child(spr)
	}

	inputMapInfo(){
		godot.InputMap.add_action("diy_action")

		// 读取InputMap中所有的action
		let actions= godot.InputMap.get_actions()
		actions.forEach((v,i)=>{
			godot.print(v);
			// 读取指定action对的Event
			let events = godot.InputMap.get_action_list(v)
			events.forEach((e,i)=>{
				godot.print(e);
			})
		})
	}

	inputMapSet(){
		let e = new godot.InputEventKey()
		e.scancode = godot.KEY_A
		godot.InputMap.action_add_event("ui_left",e)

		e = new godot.InputEventKey()
		e.scancode = godot.KEY_W
		godot.InputMap.action_add_event("ui_up",e)

		e = new godot.InputEventKey()
		e.scancode = godot.KEY_S
		godot.InputMap.action_add_event("ui_down",e)

		e = new godot.InputEventKey()
		e.scancode = godot.KEY_D
		godot.InputMap.action_add_event("ui_right",e)

	}

	moveParent(){
		this.add_child(new MovePlayer())
		this.add_child(new MoveKnight(200,500))
	}

	moveNode(){
		this.add_child(new MovePlayerNode())
		this.add_child(new MoveKnightNode(200,500))
	}

	moveSignal(){
		let player = new MovePlayerSignal()
		let knight = new MoveKnightSignal(200,500)

		// 信号节点
		let move = new MoveSignal(200,200,false,true,true)
		move.connect(MoveSignal.onUpdatePosition,(x,y)=>{
			console.log("signal",x,y)
			player.position = new godot.Vector2(x,y)
			knight.position = new godot.Vector2(x,y+300)
		})

		this.add_child(player)
		this.add_child(knight)
		this.add_child(move)
	}
	

	initArea(){
		let coinObj = new CoinArea2D();
		this.add_child(coinObj);
		coinObj.setConnect();

		let iconObj = new CoinArea2D(300,300,"res://icon.png");
		this.add_child(iconObj);
		// iconObj.setConnect();

		// 信号节点
		let move = new MoveSignal(200,200,false,true,true)
		move.connect(MoveSignal.onUpdatePosition,(x,y)=>{
			iconObj.position = new godot.Vector2(x,y)
		})
		this.add_child(move)
	}

	initKobj(){
		// 添加金币区域
		let coinObj = new CoinArea2D();
		this.add_child(coinObj);
		// Kobj与AreaObj碰撞
		// 需要在AreaObj进行碰撞检测
		coinObj.setConnect();


		// Kobj
		let x = 300,y=300;
		let iconKObj = new IconKObj(x,y,"res://icon.png");
		this.add_child(iconKObj)

		let move = new MoveSignal(x,y,false,true,true);
		move.connect(MoveSignal.onUpdatePosition,(x,y)=>{
			iconKObj.position = new godot.Vector2(x,y)
		})
		this.add_child(move)
	}

	initStaticObj(){
		let coinSobj = new CoinStaticObj();
		this.add_child(coinSobj);

		let x = 300,y=300;
		let iconKObj = new IconKObj(x,y,"res://icon.png");
		this.add_child(iconKObj)


		let iconKObj2 = new IconKObj(500,500,"",false);
		this.add_child(iconKObj2)

		// let move = new MoveSignal(x,y,false,true,true);
		// move.connect(MoveSignal.onUpdatePosition,(x,y)=>{
		// 	iconKObj.position = new godot.Vector2(x,y)

		// 	// Kobj碰撞相应
		// })
		// this.add_child(move)
	}

	initRigidObj(){
		let robj = new CoinRigidObj();
		this.add_child(robj);
	}
}
